const app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
var getTags = function (tag) {
  var a = tag.split(",");
  return a;
}
module.exports = {
  getTags: getTags
};
const {
  requestDataparams
} = require('../../utils/request.js');
Page({
  data: {
    reLoad: false,
    current: 0,
    dataList: [],
    pageSize: 0,
    currentPage: 1,
    params: null,
    userId: '',
    nav_status_list: [ //nav数据
      {
        name: "全部",
        value: "0"
      },
      {
        name: "进行中",
        value: "1"
      },
      {
        name: "已完成",
        value: "2"
      },
    ],
  },
  onLoad() {
    var current = 0;
    var that = this;
    that.setData({
      current: current,
      userId: app.globalData.id || wx.getStorageSync("id"),
    });
    that.getData()
  },
  getData() {
    var that = this;
    // var current = that.data.current;
    var userId = app.globalData.id || wx.getStorageSync("id");
    if (userId) {
      wx.request({
        url: applet_park_api_url + '/api/restaurantOrders',
        method: 'get',
        data: {
          "sysUserId": userId
        },
        success: function (res) {

          if (res.data.code == 0) {
            for (var i = 0; i < res.data.data.orderList.length; i++) {
              // res.data.data.orderList[i].restaurantImage = that.getPic(res.data.data.orderList[i].restaurantImage);
              for (var j = 0; j < res.data.data.orderList[i].foods.length; j++) {
                res.data.data.orderList[i].foods[j].dishImage = that.getPic(res.data.data.orderList[i].foods[j].dishImage);
              }
            }
            that.setData({
              dataList: res.data.data.orderList,
            });

          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            });
          }
          wx.hideLoading();

        },
        fail: function (res) {
          wx.showToast({
            title: '网络异常，请稍后重试',
          })
          wx.hideLoading();

        }

      })
    }
  },
  //处理门店头像和食品头像
  getPic(e) {
    var imgUrl = e.split(',')[0];
    imgUrl = applet_park_api_url_imgUrl + '/' + imgUrl;
    //
    return imgUrl;
  },
  getFoodsPic(e) {
    var imgUrl = e.split(',');
    var foodImgUrl = [];
    var count = 0;
    for (var i = 0; i < imgUrl.length; i++) {
      count++;

      if (count < 3) {
        foodImgUrl = applet_park_api_url_imgUrl + imgUrl[i];
      } else {
        break;
      }
    }
    return foodImgUrl;
  },
  pay(e) {

    let that = this;
    var openId = app.globalData.openId || wx.getStorageSync("OPENID");
    var id = app.globalData.id || wx.getStorageSync("id");
    var orderNo = e.currentTarget.dataset.orderno;
    var totalAmount = e.currentTarget.dataset.totalprice;
    var remark = e.currentTarget.dataset.remark;
    var couponId = e.currentTarget.dataset.couponid;

    let data = {
      orderNo: orderNo,
      totalAmount: totalAmount + '00',
      sysUserId: id,
      openid: openId,
      remark: remark,
      couponId: couponId,
      paySource: 1
    };
    var TOKEN = app.globalData.TOKEN || wx.getStorageSync("TOKEN");
    wx.request({
      url: applet_park_api_url + '/api/wechatPay',
      method: 'POST',
      header: {
        Authorization: TOKEN
      },
      data: {
        orderNo: orderNo,
        totalAmount: totalAmount + '00',
        sysUserId: id,
        openid: openId,
        remark: remark,
        couponId: couponId,
        paySource: 1
      },
      success(res2) {
        if (res2.data.code == 0) {
          wx.requestPayment({
            timeStamp: res2.data.data.timeStamp,
            nonceStr: res2.data.data.nonceStr,
            package: res2.data.data.prepay_id,
            signType: 'MD5',
            paySign: res2.data.data.sign,
            paySource: 1,
            success(res) {
              wx.showToast({
                title: '支付成功',
              })
              setTimeout(function () {
                wx.showToast({
                  title: '页面跳转中',
                })
                that.getData()
              }, 1000);
            },
            fail(res3) {
              console.log(res3)
            }
          })

        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })

  },
  onShow() {

  },
  // 下拉刷新
  onPullDownRefresh() {
    // var currentPage =1;
    // this.setData({
    //   currentPage: 1
    // });
    // that.getData(current)
  },
  // 上拉加载更多
  loadMore(e) {
    // 
    // var currentPage = 1;
    // this.setData({
    //   currentPage: currentPage+1
    // });
    //that.getData(current)
  },
  // 导航事件
  nav_event(e) {
    var that = this;
    this.setData({
      current: e.currentTarget.dataset.index,
    });
    that.getData();
  },
  //进入订单详情页面
  enterOrderDetails(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../orderDetails_canteen/orderDetails_canteen?orderNo=" + id
    })
  },
  gorestaurantevaluation(e) {
    var orderNo = e.currentTarget.dataset.orderno;
    var restaurantId = e.currentTarget.dataset.restaurantid;
    wx.navigateTo({
      url: "../restaurantevaluation/restaurantevaluation?orderNo=" + orderNo + '&restaurantId=' + restaurantId
    })
  },
});